package org.hadoop.udf;
import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters;
import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
import org.apache.hadoop.io.IntWritable;
public class AddFun extends GenericUDF {
    private ObjectInspectorConverters.Converter converter1;
    private ObjectInspectorConverters.Converter converter2;
    @Override
    public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
        if (arguments.length != 2) {
            throw new UDFArgumentException("需要两个参数，但只有：" + arguments.length);
        }
        if (arguments[0].getCategory() != ObjectInspector.Category.PRIMITIVE) {// 判断是否是基本类型
            throw new UDFArgumentException("第一个参数，不是基本类型,而是：" + arguments[0].getTypeName());
        }
        if (arguments[1].getCategory() != ObjectInspector.Category.PRIMITIVE) {
            throw new UDFArgumentException("第二个参数，不是基本类型,而是：" + arguments[1].getTypeName());
        }
        PrimitiveObjectInspector o1 = (PrimitiveObjectInspector) arguments[0];

        if (o1.getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.INT) {// 如果不是INT类型
            throw new UDFArgumentException("第一个参数，不是int类型，而是：" + o1.getTypeName());
        }
        o1 = (PrimitiveObjectInspector) arguments[1];
        if (o1.getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.INT) {
            throw new UDFArgumentException("第二个参数不是int类型,而是：" + o1.getTypeName());
        }
        //声明转换类型为基本的int类型,每一个参数，必须声明一个 Convert否则转换出错
        converter1 = ObjectInspectorConverters.getConverter(arguments[0],
                PrimitiveObjectInspectorFactory.writableIntObjectInspector);
        converter2 = ObjectInspectorConverters.getConverter(arguments[1],
                PrimitiveObjectInspectorFactory.writableIntObjectInspector);
        // 指定返回的类型
        return PrimitiveObjectInspectorFactory.writableIntObjectInspector;
    }
    @Override
    public Object evaluate(DeferredObject[] arguments) throws HiveException {
        Object o1 = arguments[0].get();
        Object o2 = arguments[1].get();
        System.out.println("o1:"+o1+",o2:"+o2);
        o1 =  converter1.convert(o1);
        o2 = converter2.convert(o2);
        System.out.println("转换以后:"+o1+","+o2);
        Integer a1 = ((IntWritable)o1).get();
        Integer a2 = ((IntWritable)o2).get();
        System.out.println("转成int以后为："+a1+","+a2);
        Integer a3= a1+a2;
        System.out.println("结果："+a3);
        return new IntWritable(a3);
    }

    @Override
    public String getDisplayString(String[] children) {
        return "AddFun";
    }
}
